package hu.mapro.gwtui.gxt.client.menu;

import hu.mapro.gwtui.client.Action;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.StyleInjector;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HasText;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;

public class MenuItemWidget extends SimpleContainer implements HasText {

	Anchor button;

	Action action;

	public MenuItemWidget() {
		button = new Anchor(true);

		setWidget(button);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				action.perform();
			}
		});
		
		getElement().addClassName(getStyle().menuItem());

	}

	public void setText(String label) {
		button.setText(label);
	}

	public void setAction(Action action) {
		this.action = action;
	}

	public interface MenuItemStyle extends CssResource {
		String menuItem();
	}

	public interface MenuItemResources extends ClientBundle {

		@Source("MenuItem.css")
		MenuItemStyle style();
	}

	private static MenuItemResources instance = GWT.create(MenuItemResources.class);

	private static boolean injected;

	public static MenuItemStyle getStyle() {
		if (!injected) {
			StyleInjector.inject(instance.style().getText(), true);
			injected = true;
		}
		return instance.style();
	}

	@Override
	public String getText() {
		return button.getText();
	}

}
